package com.suep.teacher_manage.application.service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.suep.teacher_manage.application.dto.AddTeacherRequestDto;
import com.suep.teacher_manage.application.dto.TeacherDto;
import com.suep.teacher_manage.application.dto.UpdateTeacherRequestDto;
import com.suep.teacher_manage.application.mapper.TeacherMapper;
import com.suep.teacher_manage.model.Teacher;
import com.suep.teacher_manage.repository.TeacherRepository;
import io.swagger.v3.oas.models.security.SecurityScheme;
import lombok.AllArgsConstructor;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

@Service
@AllArgsConstructor
public class TeacherService {

        private final TeacherRepository teacherRepository;
        private final TeacherMapper teacherMapper;
        //根据教师工号获取教师信息
        @Cacheable(cacheNames = "teacher")
        public TeacherDto getTeacherById(String id){
                Objects.requireNonNull(id,"id cannot be null");

                //build mybatis-plus query condition
            Wrapper<Teacher> query= Wrappers.<Teacher>lambdaQuery().eq(Teacher::getId,id);

            //fetch by query condition
            return teacherRepository.selectByid(query).stream().
                    map(teacherMapper::toTeacherDto).
                    findFirst()
                    .orElse(null);
        }
        //获取所有教师信息
        public List<TeacherDto> getAllTeachers(){
                return teacherRepository.selectAll().stream().
                        map(teacherMapper::toTeacherDto).
                        toList();
        }
        //新增教师信息
        @CachePut(cacheNames = "teacher",key = "#result.id")
        @Transactional
        public TeacherDto addTeacher(AddTeacherRequestDto addTeacherRequestDto){
            //do validation
            Objects.requireNonNull(addTeacherRequestDto,"addTeacherRequestDto cannot be null");
            //build teacher object
            var targertTeacher = Teacher.builder()
                    .id(addTeacherRequestDto.getId())
                    .name(addTeacherRequestDto.getName())
                    .institution(addTeacherRequestDto.getInstitution())
                    .department(addTeacherRequestDto.getDepartment())
                    .title(addTeacherRequestDto.getTitle())
                    .gender(addTeacherRequestDto.getGender())
                    .age(addTeacherRequestDto.getAge())
                    .telephone(addTeacherRequestDto.getTelephone())
                    .email(addTeacherRequestDto.getEmail())
                    .build();
            //save teacher object
            if (this.teacherRepository.insert(targertTeacher)<=0){
                throw new RuntimeException("Failed to add teacher");
            }else{
                return teacherMapper.toTeacherDto(targertTeacher);
            }
        }
        //删除教师信息
        @CacheEvict(cacheNames = "teacher")
        public void removeTeacher(String id){
            //do validation
            Objects.requireNonNull(id,"id cannot be null");
            //do delete
            this.teacherRepository.deleteById(id);
        }
        //更新教师信息
        @CachePut(cacheNames = "teacher",key = "#result.id")
        @Transactional
        public TeacherDto updateTeacher(String id,UpdateTeacherRequestDto updateTeacherRequestDto){
            //do validation
            Objects.requireNonNull(updateTeacherRequestDto,"updateTeacherRequestDto cannot be null");
            //build teacher object
            var targetTeacher = Teacher.builder().id(id).build();
            if(updateTeacherRequestDto.getInstitution()!=null){
                targetTeacher.setInstitution(updateTeacherRequestDto.getInstitution());
            }
            if(updateTeacherRequestDto.getDepartment()!=null){
                targetTeacher.setDepartment(updateTeacherRequestDto.getDepartment());
            }
            if (updateTeacherRequestDto.getTitle()!=null){
                targetTeacher.setTitle(updateTeacherRequestDto.getTitle());
            }
            if (updateTeacherRequestDto.getTelephone()!=null){
                targetTeacher.setTelephone(updateTeacherRequestDto.getTelephone());
            }
            if (updateTeacherRequestDto.getEmail()!=null){
                targetTeacher.setEmail(updateTeacherRequestDto.getEmail());
            }
            //update teacher object
            if (this.teacherRepository.updateById(targetTeacher)<=0){
                throw new RuntimeException("Failed to update teacher");
            }else{
                return teacherMapper.toTeacherDto(targetTeacher);
            }
        }
}
